<?php
/**
 * Clase Base DP para BaseImStockDP
 *
 * @package    
 * @subpackage BaseImStockDP
 * @author     hans
 * @version    SVN:: BaseImStockDP.class.php,v 1.1 2012-03-18 20:28:48 Hans.parra Exp $
 */

require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/common/arrayClass.class.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/im/mov/BN/ImStockBN.class.php');


abstract class BaseImStockDP extends ImStockBN
{
  /* N�mero de registros afectados */
  private $nRowsAffected;

  public function __construct($bSetDefault = true)
  {
    if( $bSetDefault instanceof ImStockBN )
    {
      $o_ImStockBN = $bSetDefault;
      if($o_ImStockBN->getAlmId()) : $this->setAlmId($o_ImStockBN->getAlmId()); endif;
      if($o_ImStockBN->getArtId()) : $this->setArtId($o_ImStockBN->getArtId()); endif;
      if($o_ImStockBN->getCantFisico()) : $this->setCantFisico($o_ImStockBN->getCantFisico()); endif;
      if($o_ImStockBN->getCantReserva()) : $this->setCantReserva($o_ImStockBN->getCantReserva()); endif;
      if($o_ImStockBN->getCantDsipon()) : $this->setCantDsipon($o_ImStockBN->getCantDsipon()); endif;
      if($o_ImStockBN->getCostoProm()) : $this->setCostoProm($o_ImStockBN->getCostoProm()); endif;
      if($o_ImStockBN->getCostoUlt()) : $this->setCostoUlt($o_ImStockBN->getCostoUlt()); endif;
      if($o_ImStockBN->getModifUsr()) : $this->setModifUsr($o_ImStockBN->getModifUsr()); endif;
      if($o_ImStockBN->getModifFecha()) : $this->setModifFecha($o_ImStockBN->getModifFecha()); endif;
    }
  }

  /* Obtiene el n�mero de registros afectados */
  public function getRowsAffected()
  {
    return $this->nRowsAffected;
  }

  public function save()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      //$sql = "SELECT IF(MAX(stk_almId),(MAX(stk_almId)+1), 1) AS Maximo FROM im_stock;";
      //$result = mysql_query($sql);
      //$row = mysql_fetch_array($result);
      //$this->setAlmId($row[0]);

      if($this->getAlmId()){
        $a_Fields = array();

        if($this->getAlmId()) : $a_Fields['stk_almId'] = ($this->getAlmId()); endif;
        if($this->getArtId()) : $a_Fields['stk_artId'] = ($this->getArtId()); endif;
        if($this->getCantFisico()) : $a_Fields['stk_cantFisico'] = ($this->getCantFisico()); endif;
        if($this->getCantReserva()) : $a_Fields['stk_cantReserva'] = ($this->getCantReserva()); endif;
        if($this->getCantDsipon()) : $a_Fields['stk_cantDsipon'] = ($this->getCantDsipon()); endif;
        if($this->getCostoProm()) : $a_Fields['stk_costoProm'] = ($this->getCostoProm()); endif;
        if($this->getCostoUlt()) : $a_Fields['stk_costoUlt'] = ($this->getCostoUlt()); endif;
        if($this->getModifUsr()) : $a_Fields['stk_modif_usr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['stk_modif_fecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_keys(',', $a_Fields);
        $sStringValue = "'".implode("','", $a_Fields)."'";

        $sql = "INSERT INTO im_stock (".$sStringField.") VALUES (".$sStringValue.");";
        $result = mysql_query($sql);
        /*
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error() );
        }
         
         */
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function update()
  {
    try
    {
      if($this->getAlmId())
      {
        $a_Fields = array();
        //if($this->getAlmId()) : $a_Fields['stk_almId'] = ($this->getAlmId()); endif;
        //if($this->getArtId()) : $a_Fields['stk_artId'] = ($this->getArtId()); endif;
        $cantFisico     = $this->getCantFisico();    
        $cantReserva    = $this->getCantReserva();  
        $cantDispon     = $this->getCantDsipon();
        $costoProm      = $this->getCostoProm();
        $costoUlt       = $this->getCostoUlt();
        if(!is_null($cantFisico)) : $a_Fields['stk_cantFisico'] = ($this->getCantFisico()); endif;
        if(!is_null($cantReserva)) : $a_Fields['stk_cantReserva'] = ($this->getCantReserva()); endif;
        if(!is_null($cantDispon)) : $a_Fields['stk_cantDispon'] = ($this->getCantDsipon()); endif;
        if(!is_null($costoProm)) : $a_Fields['stk_costoProm'] = ($this->getCostoProm()); endif;
        if(!is_null($costoUlt)) : $a_Fields['stk_costoUlt'] = ($this->getCostoUlt()); endif;
        if($this->getModifUsr()) : $a_Fields['stk_modif_usr'] = ($this->getModifUsr()); endif;
        if($this->getModifFecha()) : $a_Fields['stk_modif_fecha'] = ($this->getModifFecha()); endif;

        $sStringField = arrayClass::implode_key_value(',', $a_Fields);

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql = "UPDATE im_stock SET ".$sStringField." WHERE stk_artId ='".$this->getArtId()."'  AND stk_almId='".$this->getAlmId()."';";
        
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows();
        }
        else
        {
          throw new Exception( mysql_error() );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  public function delete()
  {
    try
    {
      if($this->getAlmId())
      {
        $a_Fields = array();

        $o_conexion = new BaseDatos();
        $con = $o_conexion->conectar();
        $sql = "DELETE FROM im_stock WHERE 1 AND stk_almId='".$this->getAlmId()."';"; 
        $result = mysql_query($sql);
        if($result)
        {
          $this->nRowsAffected = (int) mysql_affected_rows($oLink);
        }
        else
        {
          throw new Exception( mysql_error($oLink) );
        }
      }
      else
      {
        throw new Exception( 'El id no esta registrado.');
      }
    }
    catch ( Exception $o_Error )
    {
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
    return true;
  }

  /* B�squeda del registro por la llave primaria */
  public static function selectByPK($vAlmId)
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT stk_almId, stk_artId, stk_cantFisico, stk_cantReserva, stk_cantDsipon, stk_costoProm, stk_costoUlt, stk_modif_usr, stk_modif_fecha FROM im_stock WHERE 1 AND stk_almId='".$vAlmId."'; ";
      //$sql = "SELECT stk_almId, stk_artId, stk_cantFisico, stk_cantReserva, stk_cantDsipon, stk_costoProm, stk_costoUlt, stk_modifUsr, stk_modifFecha FROM im_stock WHERE 1 AND stk_almId='".$vAlmId."' and stk_artId ='".$vArtId."'"; ";

      $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      
      return $o_Propel?self::insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_ImStockDP = new ImStockBN();
    $o_ImStockDP->setAlmId($o_propel->stk_almId);
    $o_ImStockDP->setArtId($o_propel->stk_artId);
    $o_ImStockDP->setCantFisico($o_propel->stk_cantFisico);
    $o_ImStockDP->setCantReserva($o_propel->stk_cantReserva);
    $o_ImStockDP->setCantDsipon($o_propel->stk_cantDsipon);
    $o_ImStockDP->setCostoProm($o_propel->stk_costoProm);
    $o_ImStockDP->setCostoUlt($o_propel->stk_costoUlt);
    $o_ImStockDP->setModifUsr($o_propel->stk_modifUsr);
    $o_ImStockDP->setModifFecha($o_propel->stk_modifFecha);
    return $o_ImStockDP;
  }
}